package com.yanq.liancar.activity;

import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.PersistableBundle;
import android.view.View;
import android.widget.TextView;

import com.amap.api.maps.AMap;
import com.amap.api.maps.AMapOptions;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.UiSettings;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.animation.Animation;
import com.amap.api.maps.model.animation.ScaleAnimation;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.vondear.rxtool.view.RxToast;
import com.yanq.liancar.R;
import com.yanq.liancar.activity.base.BaseToolbarActivity;
import com.yanq.liancar.bean.FindOdbBindBean;
import com.yanq.liancar.bean.OdbCheckCarStatusBean;
import com.yanq.liancar.bean.UserBean;
import com.yanq.liancar.url.Config;
import com.yanq.liancar.util.LogUtils;
import com.yanq.liancar.util.ToastUtil;
import com.yanq.liancar.util.Utils;

import butterknife.BindView;
import butterknife.ButterKnife;

public class CarStatusActivity extends BaseToolbarActivity implements AMap.OnMarkerClickListener {
    @BindView(R.id.mapview)
    MapView mapview;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    @BindView(R.id.tv_speed)
    TextView tvSpeed;
    @BindView(R.id.tv_obdstutas)
    TextView tvObdstutas;
    @BindView(R.id.tv_Refresh)
    TextView tv_Refresh;
    private AMap mAMap;
    //定义一个UiSettings对象
    private UiSettings mUiSettings;
    private LatLng markerPosition;
    private double Latitude, Longitude;
    private Marker marker;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_car_status);
        ButterKnife.bind(this);
        Utils.setStatusBar(this, true, false);
        mTvTitle.setText("车辆状态");

        Latitude = getIntent().getExtras().getDouble("lat");
        Longitude = getIntent().getExtras().getDouble("lon");

        mapview.onCreate(savedInstanceState);
        init();
        getCarMessage();

        tv_Refresh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                timer();
            }
        });
    }

    private void timer() {
        ToastUtil.showProgressDialog(this);
        /** 倒计时3秒，一次1秒 */
        new CountDownTimer(2 * 1000, 2000) {
            @Override
            public void onTick(long millisUntilFinished) {
                //倒计时的过程中回调该函数
                getCarMessage();
            }

            @Override
            public void onFinish() {
                //倒计时结束时回调该函数
                ToastUtil.dismissProgressDialog();
            }
        }.start();
    }

    /**

     * 初始化AMap对象

     */

    private void init() {

        if (mAMap == null) {

            mAMap=mapview.getMap();

            mUiSettings=mAMap.getUiSettings();
            mUiSettings.setZoomControlsEnabled(false);  //显示缩放按钮
            mUiSettings.setLogoPosition(AMapOptions.LOGO_POSITION_BOTTOM_LEFT);

            markerPosition= new LatLng(Latitude, Longitude);
            mAMap.moveCamera(CameraUpdateFactory.changeLatLng(markerPosition));
            mAMap.moveCamera(CameraUpdateFactory.zoomTo(14));

            mAMap.setOnMarkerClickListener(this);

        }

        addGrowMarker();

    }

    /**

     * 添加带生长效果marker

     */

    private void addGrowMarker() {

        MarkerOptions options = new MarkerOptions();

        options.position(markerPosition);

        options.icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(this.getResources(),R.mipmap.che)));

        marker = mAMap.addMarker(options);

        Animation markerAnimation = new ScaleAnimation(0, 1, 0, 1); //初始化生长效果动画

        markerAnimation.setDuration(1000);  //设置动画时间 单位毫秒

        marker.setAnimation(markerAnimation);



    }

    @Override
    protected void onResume() {
        super.onResume();
        mapview.onResume();
        marker.startAnimation();
    }


    @Override
    protected void onPause() {
        super.onPause();
        mapview.onPause();
    }

    @Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
        super.onSaveInstanceState(outState, outPersistentState);
        mapview.onSaveInstanceState(outState);
    }

    @Override
    public boolean onMarkerClick(Marker marker) {
        marker.startAnimation();
        return true;
    }

    //查询有没有绑定车辆
    private void getCarMessage() {
        OkGo.<String>post(Config.s + "llc/odbcheck/odbCheckCarStatus")
                .tag(this)
                .params("token", UserBean.getcToken(this))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        LogUtils.e("qeqweqe", response.body());
                        Gson gson = new Gson();
                        OdbCheckCarStatusBean info = gson.fromJson(response.body(), OdbCheckCarStatusBean.class);
                        if (info.isIsSuccess()) {
                            Latitude= Double.parseDouble(info.getData().getLatitude());
                            Longitude= Double.parseDouble(info.getData().getLongitude());
                            markerPosition= new LatLng(Latitude, Longitude);
                            mAMap.moveCamera(CameraUpdateFactory.changeLatLng(markerPosition));
                            mAMap.clear();
                            addGrowMarker();
                            tvAddress.setText(info.getData().getAddress());
                            tvSpeed.setText(info.getData().getSpeed()+"km/s");
                            if ("1".equals(info.getData().getDeviceStatus())){
                                tvObdstutas.setText("在线");
                            }else {
                                tvObdstutas.setText("离线");
                            }
                        } else {
                            RxToast.info(info.getOutputMessage());
                        }
                    }
                });
    }


}
